

function isActiveMenu(item, route) {
    const key = item.keyWord;
    if (!key) {
        return false;
    }
    const { meta, path } = route;
    let currentPath = path;
    if (meta.activeMenu) {
        currentPath = meta.activeMenu;
    }
    let activeKey = currentPath.split('/')[1];
    return key.includes(activeKey);
}

const define = {
    state: () => (
        {
            // 列表
            menuList: []
        }
    ),
    actions: {
        getMenuLength() {
            return this.menuList.length;
        },
        setMenuList(list) {
            this.menuList = list;
        },
        getTopMenuList() {
            return this.menuList.filter((item)=> {
                return !item.parentId;
            });
        },
        resetMenuActive(path) {
            this.menuList.forEach(item => {
                item.active = isActiveMenu(item, path);
            })
        },
        getActiveChildMenuList() {
            const activeMenu = this.menuList.find(item=>item.active);
            if (!activeMenu) {
                return [];
            }
            const res = [];
            const children = activeMenu.children;
            if (!children) {
                return [];
            }
            for(const c of children) {
                if (c.children) {
                    res.push(...c.children);
                }
            }
            return res;
        }
    }
}

const useStore = defineStore('layout', define);

export default useStore